package com.ruoyi.web.controller.app;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.app.domain.bo.AppGroupBo;
import com.ruoyi.app.domain.vo.AppGroupVo;
import com.ruoyi.app.service.IAppGroupService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * APP群组
 *
 * @author heike
 * @date 2023-01-29
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/group")
public class AppGroupController extends BaseController {

    private final IAppGroupService iAppGroupService;

    /**
     * 查询APP群组列表
     */
    @SaCheckPermission("system:group:list")
    @GetMapping("/list")
    public TableDataInfo<AppGroupVo> list(AppGroupBo bo, PageQuery pageQuery) {
        return iAppGroupService.queryPageList(bo, pageQuery);
    }

    /**
     * 发送APP群组消息
     */
    @Log(title = "APP群组", businessType = BusinessType.EXPORT)
    @PostMapping("/sendGroup")
    public R<Object> sendGroup(@RequestBody AppGroupBo bo) {
        AppGroupVo appGroupVo = iAppGroupService.queryById(bo.getId());
        if (Objects.isNull(appGroupVo)) {
            return R.fail("群组不存在!");
        }
        if (StringUtils.isBlank(bo.getContext())) {
            return R.fail("发送内容不能为空！");
        }
        Set<String> toGroups = new HashSet<>();
        toGroups.add(appGroupVo.getGroupId());
        return R.ok("发送成功");
    }


    /**
     * 导出APP群组列表
     */
    @SaCheckPermission("system:group:export")
    @Log(title = "APP群组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppGroupBo bo, HttpServletResponse response) {
        List<AppGroupVo> list = iAppGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "APP群组", AppGroupVo.class, response);
    }

    /**
     * 获取APP群组详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:group:query")
    @GetMapping("/{id}")
    public R<AppGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
        return R.ok(iAppGroupService.queryById(id));
    }

    /**
     * 新增APP群组
     */
    @SaCheckPermission("system:group:add")
    @Log(title = "APP群组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppGroupBo bo) {
        return toAjax(iAppGroupService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改APP群组
     */
    @SaCheckPermission("system:group:edit")
    @Log(title = "APP群组", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppGroupBo bo) {
        return toAjax(iAppGroupService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除APP群组
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:group:remove")
    @Log(title = "APP群组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAppGroupService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
